class OtherOrgController < ApplicationController
	before_filter :login_required
	#模版
    layout "framework",:except=>['agent_info']
    #首页
    def index	
    	@session[:rights] = buildMenu '客户管理'
    end
	def add_agent
    	if !request.get?
    	    agent = Agent.new(params[:agent])
            agent.employee_id =@session[:account].employee_id 
    	    if agent.save
    	    	flash[:notice] = "<img src='/images/ok.gif'><font color='green'>客户#{agent.name}添加成功！</font>"
    			redirect_to(:action=>'add_agent')
    	    else
    	    	redirect_to_failure()
    	    end	
    	end
	end
	
	def edit_agent
		if request.get?
			@agent = Agent.find(params[:id])
		else
			agent = Agent.find(params[:id])
			agent.name = params[:name]
			agent.part = params[:part]
			agent.person = params[:person]
			agent.sex = params[:sex]
			agent.birthday_on = params[:birthday_on]
			agent.phone = params[:phone]
			agent.mobile = params[:mobile]
			agent.lingtong = params[:lingtong]
			agent.fax = params[:fax]
			agent.address1 = params[:address1]
			agent.address2 = params[:address2]
			agent.qq = params[:qq]
			agent.msn = params[:msn]
			agent.email = params[:email]
			agent.favor = params[:favor]
			agent.second_person = params[:second_person]
			agent.second_phone = params[:second_phone]
			agent.memo = params[:memo]
			if agent.save
				flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#客户#{agent.name}已经成功修改！</font>"
    			redirect_to(:action=>'list_agent')
			end
		end
	end
	
	def list_agent
		@agents = Agent.find_all
	end
	
	def del_agent
		Agent.delete(params[:id])
                flash[:notice] = "<img src='/images/ok.gif'><font color='green'>删除成功！</font>"
		redirect_to(:action=>'list_agent')
	end
	
	def show_agent
		@agent = Agent.find(params[:id])
	end
	
	def agent_info
		show_agent
	end
	
	def add_agent_family
		if request.get?
    		@family = AgentFamilyMember.new
    		@agents = Agent.find_all
    	else
    		@family = AgentFamilyMember.new(params[:family]) 
    		if @family.save
    			flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{@family.name}已经成功增加！</font>"
    			redirect_to(:action=>'add_agent_family')
    		end
    	end
	end
	
	def list_agent_family
		@agents = Agent.find_all
	end
	
	def list_family
		agent_id = params[:agent_id]
		@familys = AgentFamilyMember.find(:all,:conditions=>"agent_id='#{agent_id}'",:order=>'birthday DESC')
	end
	
	def edit_agent_family
		if request.get?
			@family = AgentFamilyMember.find(params[:id])
		else
			family = AgentFamilyMember.find(params[:id])
			family.appellation = params[:appellation]
			family.name = params[:name]
			family.sex = params[:sex]
			family.birthday = params[:birthday]
			if family.save
				flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{family.name}已经成功修改！</font>"
    			redirect_to(:action=>'list_agent_family')
			end
		end
	end
	
	def del_agent_family
		AgentFamilyMember.delete(params[:id])
    	flash[:notice] = "<img src='/images/ok.gif'><font color='green'>删除成功！</font>"
    	redirect_to(:action=>'list_agent_family')
	end
end
